/**
 * desc：
 * author：王文军
 * date：  2022/3/20 19:30
 */
const {resolve} = require('path')
const {statSync} = require('fs')
class BundlesizeWebpackPlugin {
  constructor(options) {
    this.options = options
  }
  apply(compiler){
    const {sizeLimit} = this.options
    console.log('bundle size plugin')
    // compile代表编译阶段的生命周期
    compiler.hooks.compile.tag('BundleSizePlugin',(compilationParams) => {
      console.log('compile',compilationParams);
    })
    // done代表整个项目编译结束的生命周期
    compiler.hooks.done.tag('BundleSizePlugin',(status) => {
      console.log('done',status);
      const {path,filename} = status.compilation.outputOptions
      const bundlePath = resolve(path,filename)
      const {size} = statSync(bundlePath)
      const bundleSize  = size / 1024
      if (bundleSize < size){
        console.log('123');
      } else {
        console.log('456');
      }
    })
  }
}
module.exports = BundlesizeWebpackPlugin
